package game.poker.holdem.entity;

import game.poker.holdem.HoldEmUtils;
import game.poker.holdem.card.Card;
import game.poker.holdem.hand.Hand;

import java.util.HashSet;
import java.util.Set;

public class Player {
    private String name;
    private Set<Card> holeCards;
    private Hand bestHand;
    
    private Table table;
    
    public Player(String name) {
        this.name = name;
        holeCards = new HashSet<Card>();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public void forfeitTable() {
        if(!table.removePlayer(this)) {
            throw new RuntimeException("Player wasn't seated in the table!");
        }
        table = null;
    }

    public Set<Card> getHoleCards() {
        return holeCards;
    }
    
    public void takeCard(Card card) {
        if(holeCards.size() > 2) {
            throw new IllegalStateException("Player already has 2 hole cards!");
        }
        holeCards.add(card);
    }
    
    public Set<Card> returnCards() {
        Set<Card> cards = holeCards;
        holeCards = new HashSet<Card>();
        return cards;
    }


    public Hand getBestHand() {
        return bestHand;
    }

    public void setBestHand(Hand bestHand) {
        this.bestHand = bestHand;
    }
    
    public boolean sit(Table table) {
        boolean seated = table.addPlayer(this);
        if(seated) {
            this.table = table;            
        }
        return seated;
    }
    
    public String holeCardsToStringUnicodeImages() {
        StringBuilder sb = new StringBuilder();
        for(Card hC : holeCards) {
            sb.append(HoldEmUtils.toStringUnicodeImage(hC));
        }
        return sb.toString();
    }

	@Override
	public String toString() {
		return name;
	}   
}
